package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.AppAdminDao;
import com.pojo.AppAdmin;
import com.util.PasswordUtil;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppAdminService extends ServiceImpl<AppAdminDao, AppAdmin> {

    @Autowired
    private AppAdminDao appAdminDao;

    /**
     * 修改密码
     * @param opwd
     * @param npwd
     * @param id
     * @return
     */
    public String appUpdatePassword(String opwd, String npwd, String id) {
        AppAdmin wuye = super.getById(id) ;
        if(!PasswordUtil.jiami(opwd).equals(wuye.getPassword())) {
            return "原始密码错误" ;
        }
        wuye.setPassword(PasswordUtil.jiami(npwd));
        super.updateById(wuye) ;
        return null ;
    }

    public IPage findRoleByAdmin(int page, int size, String text) {
        IPage iPage = new Page(page, size);
        QueryWrapper<AppAdmin> query = new QueryWrapper();
        if(StringUtil.isNotNull(text)){
            query.and(wrapper->wrapper.like("username",text).or().like("mail",text).or().like("phone",text));
        }
        IPage<AppAdmin> maps = appAdminDao.selectPage(iPage, query);
        return maps;
    }
}
